﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.Script.Serialization;

namespace ClientDemoV1
{
    public partial class FClientDemo : Form
    {
        public FClientDemo()
        {
            InitializeComponent();
        }

        private void textBoxXMLFileName_DoubleClick(object sender, EventArgs e)
        {
            textBoxXMLFileName.Text = "";
            textBoxSSize.Text = "";
            textBoxBCount.Text = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBoxXMLFileName.Text = openFileDialog1.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filePath = textBoxXMLFileName.Text;
            if (File.Exists(filePath) && Path.GetExtension(filePath).ToUpper().Equals(".XML"))
            {
                FileInfo tFileInfo = new FileInfo(filePath);
                textBoxSSize.Text= tFileInfo.Length.ToString();
                StringBuilder tFileStr = new StringBuilder();
                tFileStr.Append(File.ReadAllText(filePath));
                StringBuilder tBase64FileStr = new StringBuilder();

                tBase64FileStr.Append(Convert.ToBase64String(Encoding.UTF8.GetBytes(tFileStr.ToString())));
                textBoxBCount.Text = tBase64FileStr.Length.ToString();
                richTextBoxContext.AppendText(tBase64FileStr.ToString());

                Dictionary<string, object> tValue= new Dictionary<string, object>();
                tValue.Add("XMLFile", Convert.ToBase64String(Encoding.UTF8.GetBytes(tFileStr.ToString())));

                JavaScriptSerializer tSerializer = new JavaScriptSerializer();
                string sJSONStr= tSerializer.Serialize(tValue);

                richTextBoxContext.Text = sJSONStr;

                tValue.Clear();
                Dictionary<string, object> tXValue = (Dictionary<string, object>)tSerializer.DeserializeObject(sJSONStr);
                //object[] tDatas =(object[]) tXValue["XMLFile"];
            }
        }

        private void buttonGetStr_Click(object sender, EventArgs e)
        {
            StringBuilder tBase64FileStr = new StringBuilder();
            tBase64FileStr.Append(richTextBoxContext.Text);
            richTextBoxContext.Clear();
            richTextBoxContext.AppendText(Encoding.UTF8.GetString(Convert.FromBase64String(tBase64FileStr.ToString())));
        }
    }
}
